package com.bw.home.viewmodel

import android.content.Context
import android.util.Log
import com.bw.home.entity.SimpleVideoEntity
import com.bw.home.intent.HomeIntent
import com.bw.home.repo.HomeRepo
import com.bw.lib_miv.BaseViewModel
import com.bw.lib_miv.IUiState
import com.bw.lib_miv.RequestType
import com.bw.lib_net.BaseRes
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject


@HiltViewModel
class HomeViewModel @Inject constructor(
    private val repo: HomeRepo,
    @ApplicationContext val context: Context
) : BaseViewModel<HomeIntent, IUiState<Any>>() {
    override fun handlerIntent(intent: HomeIntent) {
        when (intent) {
            //类型
            is HomeIntent.getSimpleType -> {
                httpRequest(repo.getSimpleType(), RequestType.SIMPLE_TYPE)
            }

            //类型数据
            is HomeIntent.getSimpleVideo -> {
                httpRequest(
                    repo.getSimpleVideo(intent.id, intent.page, intent.size),
                    RequestType.SIMPLE_VIDEO
                )
            }

            //猜您喜欢
            is HomeIntent.getRecommendSimpleVideo -> {
                httpRequest(
                    repo.getRecommendSimpleVideo(intent.page, intent.size),
                    RequestType.RECOMMEND_SIMPLE_VIDEO
                )
            }

            //评论
            is HomeIntent.getComment -> {
                httpRequest(repo.getComment(intent.type, intent.id), RequestType.COMMENT)
            }

            //收藏
            is HomeIntent.getCollect -> {
                httpRequest(repo.getCollect(intent.entity), RequestType.COLLECT)
            }

            //取消收藏
            is HomeIntent.unCollect -> {
                httpRequest(repo.unCollect(intent.entity), RequestType.UN_COLLECT)
            }

            //登录
            is HomeIntent.login -> {
                httpRequest(repo.login(intent.username, intent.password), RequestType.LOGIN)
            }

            //注册
            is HomeIntent.register -> {
                httpRequest(repo.register(intent.username, intent.password), RequestType.REGISTER)
            }

            //修改密码
            is HomeIntent.updatePassword->{
                httpRequest(repo.updatePassword(intent.userId,intent.newPassword),RequestType.UPDATE_PASSWORD)
            }

            else -> {}
        }
    }

    //    var videoItem: SimpleVideoEntity?
//        get() = repo.tempItem
//        set(value) {
//            repo.tempItem = value
//        }
    fun getVideoItem() = repo.getVideoItem()
    fun setList(item: SimpleVideoEntity) {
        repo.setVideoItem(item)
    }
}